package.path = "./plugin/ADL200/utils.lua"..";..\\?.lua"
package.path = "./plugin/ADL200/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GetVariables(sAddr,step)
    local requestADU = {}
    local crc

    if(step == 0)
    then
        -----------读电能信息----------------
        RegAddr = 0
        RegCnt = 18
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = math.modf(RegAddr/256)
        requestADU[4] = math.modf(RegAddr%256)
        requestADU[5] = math.modf(RegCnt/256)
        requestADU[6] = math.modf(RegCnt%256)

        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)
    end

    return requestADU

end

function GenerateGetRealVariables(sAddr,step)

    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    requestADU = GetVariables(sAddr,step)

    regAddr = 0
    VariableMap = {
        Status = "0",
        Variable = requestADU
    }

    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(cmdName == "GetRealVariables")
    then
        requestADU = GetVariables(sAddr,step)

        regAddr = 0
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH,crcL,crc,crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if(rxBufCnt < 6)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            mAddr     = rxBuf[index]
            mFunCode  = rxBuf[index+1]
            if (mAddr == tonumber(sAddr))
            then
                if(mFunCode == 0x03)
                then
                    mRegByte = rxBuf[index+2]
                    if(mRegByte+5 > rxBufCnt)
                    then
                        rxBuf = {}
                        return VariableMap
                    end
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+2+mRegByte,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                    if(crc == crcTemp)
                    then
                        local Variable = {}
                        local varIndex = 3
                        local VariableIndex = 1
                        if(regAddr == 0)
                        then
                            ---------------------------有功总电能----------------------------------
                            local TotalE = rxBuf[index+varIndex]*16777216+rxBuf[index+varIndex+1]*65536+rxBuf[index+varIndex+2]*256+rxBuf[index+varIndex+3]
                            varIndex = varIndex + 4
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"TotalE",TotalE,TotalE)
                            VariableIndex = VariableIndex + 1
                            ---------------------------有功尖电能，保留----------------------------------
                            varIndex = varIndex + 4
                            ---------------------------有功峰电能，保留----------------------------------
                            varIndex = varIndex + 4
                            ---------------------------有功平电能，保留----------------------------------
                            varIndex = varIndex + 4
                            ---------------------------有功谷电能，保留----------------------------------
                            varIndex = varIndex + 4
                            ---------------------------密码，保留----------------------------------
                            varIndex = varIndex + 2
                            ---------------------------电压----------------------------------
                            local U = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"U",U,U)
                            VariableIndex = VariableIndex + 1
                            ---------------------------电流----------------------------------
                            local I = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"I",I,I)
                            VariableIndex = VariableIndex + 1
                            ---------------------------有功功率----------------------------------
                            local P = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"P",P,P)
                            VariableIndex = VariableIndex + 1
                            ---------------------------无功功率----------------------------------
                            local Q = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Q",Q,Q)
                            VariableIndex = VariableIndex + 1
                            ---------------------------视在功率----------------------------------
                            local S = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"S",S,S)
                            VariableIndex = VariableIndex + 1
                            ---------------------------功率因素----------------------------------
                            local PF = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"PF",PF,PF)
                            VariableIndex = VariableIndex + 1
                            ---------------------------频率----------------------------------
                            local Freq = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Freq",Freq,Freq)
                            VariableIndex = VariableIndex + 1

                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                        end
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x01 or mFunCode == 0x02)
                    then
                        mRegByte = rxBuf[index+2]
                        if(mRegByte+5 > rxBufCnt)
                        then
                            rxBuf = {}
                            return VariableMap
                        end
                        --取出数据域数据
                        local pdu = {}
                        for i=index,index+2+mRegByte,1 do
                            if rxBuf[i] ~= nil then
                                table.insert(pdu, rxBuf[i])
                            end
                        end
                        local VariableMapCRC = {
                            Variable = pdu
                        }
                        crc = GetCRCModbus(VariableMapCRC)
                        crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                        if(crc == crcTemp)
                        then
                            local Variable = {}
                            local varIndex = 3
                            local VariableIndex = 1
                            if(regAddr == 10001)
                            then

                                -------------------------------------------------------------
                                VariableMap = {
                                    Status = "0",
                                    Variable = Variable
                                }
                            end
                            rxBuf = {}
                            return VariableMap
                        end
                elseif(mFunCode == 0x05)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x06)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end    
                else
                    index = index + 1
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end

